﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebsiteAdmin.Wilson.ProInfo
{
    public partial class ProList : System.Web.UI.Page
    {
        private string strCon = Global.ConnWilson;
        private int pageSize = Global.PageSize;
        private int pageIndex = 0;
        private string whereStr = "WHERE [TProduct].[Valid] = 1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["page"] != null && Request["page"] != "")
                    pageIndex = Convert.ToInt32(Request["page"].ToString());
                DataTable dtProduct = GetProList(strCon, pageSize, pageIndex, whereStr);
                this.RP_Product.DataSource = dtProduct.DefaultView;
                this.RP_Product.DataBind();
                dtProduct.Dispose();
            }
        }

        protected DataTable GetProList(string strCon, int pageSize, int pageIndex, string whereStr)
        {
            return Bll.Product.MULTIPLE(strCon, pageSize, pageIndex, whereStr).DT;
        }

        protected void RP_Product_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                Image thumbURL = ((Image)e.Item.FindControl("ThumbURL"));

                if (rowv["MainImgURL"].ToString().Trim().Length > 0)
                {
                    thumbURL.ImageUrl = rowv["MainImgURL"].ToString();
                }
                else
                {
                    thumbURL.ImageUrl = "/Images/view.png";
                }

                Label lblRecomm = ((Label)e.Item.FindControl("lblRecomm"));
                lblRecomm.Text = ProRecommand(Global.ConnWilson, Convert.ToInt16(rowv["PKID"])) == 1 ? "是" : "否";
            }
        }

        protected int ProRecommand(string strCon,int proId)
        {
            using (DataTable dt = Bll.RecommendPro.Single(strCon, proId)) 
            {
                if (dt != null && dt.Rows.Count > 0)
                    return 1;
                else
                    return 0;
            }
        }
    }
}
